package com.gcstorage.tvdev.model

import androidx.lifecycle.MutableLiveData
import com.alibaba.fastjson.JSON
import com.blankj.utilcode.util.DeviceUtils
import com.blankj.utilcode.util.LogUtils
import com.gcstorage.tvdev.base.BaseViewModel
import com.gcstorage.tvdev.bean.LoginUserInfo
import com.gcstorage.tvdev.bean.QRCodeInfo
import com.gcstorage.tvdev.bean.QrListenerBean
import com.gcstorage.tvdev.bean.UserInfo
import com.gcstorage.tvdev.constants.AppConstant
import com.gcstorage.tvdev.constants.KeyCode
import com.gcstorage.tvdev.constants.KeyCode.USER_INFO
import com.gcstorage.tvdev.constants.LogCat
import com.gcstorage.tvdev.net.ApiAddress
import com.gcstorage.tvdev.net.HttpClient
import com.gcstorage.tvdev.net.RxObserver
import com.gcstorage.tvdev.util.GsonUtils
import com.gcstorage.tvdev.util.MD5Util.md5
import com.gcstorage.tvdev.util.SpUtils
import com.gcstorage.tvdev.util.UserHelper
import com.google.gson.JsonElement
import java.util.*

/**
 *@author: liangkun
 *@date: 2022/3/30
 *@des: denglu
 */
class LoginViewModel : BaseViewModel() {
    //登录方式 1：账号密码 2 短信
    val loginMode = MutableLiveData(1)

    fun isPasswordLogin() = loginMode.value == 1
    fun isShortMsgLogin() = loginMode.value == 2
    fun isNoPassword() = loginMode.value == 3

    //登录状态
    val loginState: MutableLiveData<Boolean> = MutableLiveData()

    //二维码数据
    val qrCode: MutableLiveData<QRCodeInfo> = MutableLiveData()

    //监听
    val qrListenerBean: MutableLiveData<QrListenerBean> = MutableLiveData()

    //短信验证码
    val code: MutableLiveData<Boolean> = MutableLiveData()

    /**
     * 虚拟岗列表
     */
    val userList:MutableLiveData<List<UserInfo>> = MutableLiveData()



     fun getUserInfo() {
        val params = HashMap<String, Any>()
        params["userId"] = SpUtils.getString(SpUtils.TAG_USER, KeyCode.USER_ID, "")
        val url: String = AppConstant.getApiAddress() + ApiAddress.USER_INFO
        HttpClient.get().get(url, params, object : RxObserver() {
            override fun onSuccess(t: JsonElement?) {
                val userInfo = GsonUtils.covertObject(
                    t,
                    UserInfo::class.java
                ).data
                UserHelper.instance().loginInUser(userInfo)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_NAME, userInfo.realName)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_ID_CARD, userInfo.idCard)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_POLICE_NUM, userInfo.policeNo)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_DEPART_NAME, userInfo.deptName)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_DEPART_ID, userInfo.deptId)
                SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_PHONE, userInfo.phone)
                loginState.value = true
            }

            override fun onFinish() {
                super.onFinish()
                loginState.value = false
            }
        })
    }

    /**
     * 生成二维码
     */
    fun createQrCode() {
        LogUtils.d("apiADDRESS ${AppConstant.getApiAddress()}")
        HttpClient.get().post(AppConstant.getApiAddress() + ApiAddress.AUTH_QRCODE_CREATE,
            Collections.singletonMap(
                "ip",
                DeviceUtils.getManufacturer() + " " + DeviceUtils.getModel()
            ) as Map<String, Any>?, object : RxObserver() {
                override fun onSuccess(t: JsonElement) {
                    LogUtils.d(LogCat.LOG_COMMONON + " onSuccess ")
                    var qrCodeInfo: QRCodeInfo =
                        GsonUtils.covertObject(t, QRCodeInfo::class.java).data
                    qrCode.value = qrCodeInfo

                }

                override fun onFail() {
                    super.onFail()
                    LogUtils.d(LogCat.LOG_COMMONON + " onFinish ")
                    qrCode.value = null
                }
            })

    }

    //监听二维码扫码
    fun listenerQrCode(uid: String) {
        HttpClient.get().post(AppConstant.getApiAddress() + ApiAddress.AUTH_QRCODE_LISTENER,
            Collections.singletonMap("uid", uid) as Map<String, Any>?, object : RxObserver() {
                override fun onSuccess(t: JsonElement) {
                    qrListenerBean.value =
                        GsonUtils.covertObject(t, QrListenerBean::class.java).data

                }
            })
    }

    fun qrLogin(uid: String, userId: String) {
        val params = HashMap<String, Any>()
        params["uid"] = uid
        params["userId"] = userId
        params["User-Type"] = "tv"
        HttpClient.get().post(
            AppConstant.getApiAddress() + ApiAddress.AUTH_QRCODE_LOGIN,
            params,
            object : RxObserver() {
                override fun onSuccess(t: JsonElement) {
                    val loginUserInfo = GsonUtils.covertObject(t, LoginUserInfo::class.java).data
                    LogUtils.d(LogCat.LOG_COMMONON + " client_user_id ${loginUserInfo.client_user_id}")
                    SpUtils.putString(
                        SpUtils.TAG_USER,
                        KeyCode.USER_ACCESS_TOKEN,
                        loginUserInfo.access_token
                    )
                    SpUtils.putString(
                        SpUtils.TAG_USER,
                        KeyCode.USER_TOKEN_TYPE,
                        loginUserInfo.token_type
                    )
                    SpUtils.putString(
                        SpUtils.TAG_USER,
                        KeyCode.USER_ID,
                        loginUserInfo.client_user_id
                    )
                    SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_AVATAR, loginUserInfo.avatar)
                    SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_PHONE, loginUserInfo.phone)
                    getUserInfo()
                }
            })
    }





}